package org.bingo.service;

import cn.dev33.satoken.stp.StpUtil;
import org.bingo.entity.Comment;
import org.bingo.entity.User;
import org.bingo.entity.Video;
import org.bingo.repository.CommentRepository;
import org.bingo.repository.UserRepository;
import org.bingo.repository.VideoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {

    @Autowired
    private VideoRepository videoRepository;

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private UserRepository userRepository;

    @Transactional
    public Comment getCommentById(Long id) {
        Comment comment = commentRepository.findById(id).orElse(null);
        comment.getUser();
        return comment;
    }

    @Transactional
    public List<Comment> getLevel1Comment(Long videoId,
                                          Integer page,
                                          Integer countPerPage) {
        Video video = videoRepository.findById(videoId).orElse(null);
        if (video == null) {
            return null;
        }
        Page<Comment> pageComment = commentRepository.findByCommentLevelAndVideo(0, video,
                PageRequest.of(page, countPerPage));
        return pageComment.getContent();
    }

//    @Transactional
//    public List<Comment> getLevel2Comment(Long commentId) {
//        Comment comment = commentRepository.findById(commentId).orElse(null);
//        if (comment == null) {
//            return new ArrayList<>();
//        }
//
//        return comment.getSubComment();
//    }

    @Transactional
    public Long addLevel1Comment(Long videoId, String context) {
        Video video = videoRepository.findById(videoId).orElse(null);
        if (video == null) {
            return -1L;
        }
        User user = userRepository.findById(StpUtil.getLoginIdAsLong()).orElse(null);
        if (user == null) {
            return -2L;
        }
        Comment comment = new Comment();
        comment.setComment(context);
        comment.setUser(user);
        comment.setVideo(video);
        comment.setCommentLevel(0);
        commentRepository.save(comment);
        return comment.getId();
    }


//    @Transactional
//    public Long addLevel2Comment(Long commentId, String context) {
//        Comment level1Comment = commentRepository.findById(commentId).orElse(null);
//        if (level1Comment == null) {
//            return -1L;
//        }
//        User user = userRepository.findById(StpUtil.getLoginIdAsLong()).orElse(null);
//        if (user == null) {
//            return -2L;
//        }
//        Comment level2Comment = new Comment();
//        level2Comment.setComment(context);
//        level2Comment.setUser(user);
//        level2Comment.setVideo(level1Comment.getVideo());
//        level2Comment.setCommentLevel(1);
//        level2Comment.setParentComment(level1Comment);
//        commentRepository.save(level2Comment);
//        return level2Comment.getId();
//    }

    @Transactional
    public Long delComment(Long commentId) {
        Comment comment = commentRepository.findById(commentId).orElse(null);
        if (comment == null) {
            return -1L;
        }
        if (!comment.getUser().getId().equals(StpUtil.getLoginIdAsLong())) {
            return -2L;
        }
        commentRepository.delete(comment);
        return 0L;
    }

}
